;;; htmlize-view.el --- View current buffer as html in web browser

;; Copyright (C) 2005, 2006, 2007 by Lennart Borgman

;; Author: Lennart Borgman
;; Created: Fri Oct 21 00:11:07 2005
;; Version: 0.62
;; Last-Updated: Sat Mar 31 12:46:17 2007 (7200 +0200)
;; Keywords: printing
;; URL: http://OurComments.org/Emacs/DL/elisp/htmlize-view.el
;; Compatibility:
;;
;;
;; Features that might be required by this library:
;;
;;   `button', `cl', `custom', `easymenu', `help-fns', `help-mode',
;;   `htmlize', `view', `widget'.
;;
;;
;; You can find htmlize.el at
;;   http://fly.srk.fer.hr/~hniksic/emacs/htmlize.el
;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;;  This file shows the current buffer in your web browser with all
;;  the colors it has. The purpose is mainly to make it possible to
;;  easily print what you see in Emacs in colors on different
;;  platforms.
;;
;;  Put this file in your load-path and in your .emacs this:
;;
;;      (require 'htmlize-view)
;;      (htmlize-view-add-to-files-menu)
;;
;;  This will add an entry "Quick Print (to OS Default Printer) on the
;;  files menu.  This menu entry can be hidden, see the setting
;;  `htmlize-view-print-visible'.
;;
;;  Use this or call `htmlize-view-buffer' to show the current buffer
;;  in your web browser.
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Change log:
;;
;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2, or (at
;; your option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.
;;
;; To find out more about the GNU General Public License you can visit
;; Free Software Foundation's website http://www.fsf.org/.  Or, write
;; to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:


(require 'htmlize)
(require 'easymenu)

(defvar htmlize-view-kill-view-buffers t
  "If non-nil delete temporary html buffers after sending to browser.")

(defun htmlize-view-region()
  "Convert retion to html preserviing faces and view in web browser."
  (interactive)
  (htmlize-view-buffer t))

(defun htmlize-view-buffer(&optional region-only)
  "Convert buffer to html preserving faces and view in web browser.
If REGION-ONLY is non-nil then only the region is sent to the web browser."
  (interactive)
  (message "Creating a html version for printing ...")
  (let* ((html-file (htmlize-buffer-to-tempfile region-only)))
    (browse-url-of-file html-file)
    ))

(defun htmlize-buffer-to-tempfile(region-only)
  "Convert buffer to html preserving faces and write to a temporary file.
If REGION-ONLY is non-nil then only the region is sent to the web browser.
Return a cons with temporary file name followed by temporary buffer."
  (save-excursion
    (let (;; Use Fundamental mode for the temp buffer
          magic-mode-alist
          auto-mode-alist
          (html-temp-buffer
           (if (not region-only)
               (htmlize-buffer (current-buffer))
             (let ((start (mark)) (end (point)))
               (or (<= start end)
                   (setq start (prog1 end (setq end start))))
               (htmlize-region start end))))
          (file (htmlize-view-gettemp-file-name)))
      (set-buffer html-temp-buffer)
      (write-file file nil)
      (if htmlize-view-kill-view-buffers (kill-buffer html-temp-buffer))
      file)))

(defcustom htmlize-view-temp-dir temporary-file-directory
  "The name of a directory for htmlize-view's temporary files.
Such files are generated by functions like
`htmllize-view-buffer'.  You might want to set this to somewhere
with restricted read permissions for privacy's sake."
  :type 'string
  :group 'htmlize)

(defun htmlize-view-gettemp-file-name()
  "Get a temp file name for printing."
  (make-temp-file
   (expand-file-name "htmlize-view" htmlize-view-temp-dir) nil ".htm"))

(defun htmlize-view-gettemp-file-name-21()
  "Get a temp file name for printing.
Use this instead of `htmlize-view-gettemp-file-name' if you are
trying to use this file with Emacs 21. Just rename that function
and this function to do that."
  (concat (make-temp-file
           (expand-file-name "htmlize-view" htmlize-view-temp-dir) nil)
          ".htm"))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;; Menus

(defvar htmlize-view-print-menu (make-sparse-keymap "QP"))
(defvar htmlize-view-print-region-menu (make-sparse-keymap "QPR"))
(defun htmlize-view-add-to-files-menu()
  "Add \"Quick Print\" entry to file menu."
  (setq htmlize-view-print-menu (make-sparse-keymap "QP"))
  (setq htmlize-view-print-region-menu (make-sparse-keymap "QPR"))
  ;; Main
  (define-key-after menu-bar-file-menu [htmlize-view-print]
    (list 'menu-item
          "Quick Print (to OS Default Printer)"
          htmlize-view-print-menu
          :visible 'htmlize-view-print-visible)
    'separator-print)
  ;; Main submenu
  (define-key htmlize-view-print-menu [htmlize-view-region]
    (list 'menu-item "Print Region" htmlize-view-print-region-menu
          :help "Print only region"
          :enable 'mark-active))
  (define-key htmlize-view-print-menu [htmlize-view-separator-pre]
    '(menu-item "--"))
  (define-key htmlize-view-print-menu [htmlize-view-browser-pre]
    '(menu-item "Preview with Web Browser" htmlize-view-buffer
                :help "Print preview buffer with web browser"
                :visible t))
  ;; Submenu region
  (define-key htmlize-view-print-region-menu [htmlize-view-browser-region-pre]
    '(menu-item "Preview with Web Browser" htmlize-view-region
                :help "Print preview region with web browser"))
  )

(defvar htmlize-view-print-visible t
  "Non-nil means show Quick Print entry on the file menu.")


(provide 'htmlize-view)

;;; htmlize-view.el ends here
